var DoctorRevisit = {
    id: "DoctorRevisitTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus == 2) {
        return {css: {"background-color": "#fff1d5", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

DoctorRevisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 100,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick='DoctorRevisit.lookDetailtcorqt(" + '"' + row.customerId + '"' + ")'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建人', field: 'createName', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 200,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访结果', field: 'revisitContent', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return value === 1 ? '待计划' : value === 2 ? '回访完成' : value === 3 ? '回访未完成' : value === 4 ? '回访取消' : ''
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: '', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (row.revisitStatus == 1) {
                    return '<button title="删除随访" onclick="DoctorRevisit.deleteAppointment(\'' + row.id + '\',\'' + row.customerName + '\')"' +
                        ' type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;">' +
                        '<span class="glyphicon glyphicon-trash"></span></button>';
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
    ];
    return columns;
};

//删除随访
DoctorRevisit.deleteAppointment = function (id, customerName) {//删除随访
    if (confirm("患者【" + customerName + "】确认删除回访?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/delete_revisit",
            type: "post",
            data: {
                id: id
            },
            success: function (data) {
                if (data == -1) {
                    alert("亲，不能取消其他医生的随访！");
                    return;
                } else {
                    Feng.success('操作成功!');
                    window.DoctorRevisit.search();
                }
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
};


DoctorRevisit.lookDetailtcorqt = function (id) {//点击姓名查看详情
    var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
    if (currentSeat.indexOf("到店") != -1) {
        $.ajax({
            url: Feng.ctxPath + "/consultantTwo/getdcsByCustomerId",
            type: "post",
            data: {
                "id": id
            },
            success: function (data) {
                if (data == 1) {
                    var index = layer.open({
                        type: 2,
                        title: '到店客户详情',
                        area: ['100%', '100%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + id + "/0"
                    });
                    this.layerIndex = index;
                } else {
                    alert("请先联系前台客服匹配建档！");
                    return;
                }
            }
        });
    } else {
        var index = layer.open({
            type: 2,
            title: '&nbsp',
            area: ['100%', '100%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/0/0'
        });
        this.layerIndex = index;
    }


};

DoctorRevisit.followRevision = function (ids) {//点击表格随访按钮
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/doctorRevisit/doctor_follow/' + ids
    });
    this.layerIndex = index;
};

DoctorRevisit.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        DoctorRevisit.seItem = selected[0];
        return true;
    }
};

DoctorRevisit.search = function () {//查询预约客户列表
    DoctorRevisit.table.refresh({query: DoctorRevisit.formParams()});
    DoctorRevisit.total();
};

DoctorRevisit.total = function () {//人数统计
    var reach = $("#doctorTcInputPublic", parent.document).val();
    var revisitStatus = $("#revisitStatus").val();  //回访状态
    var startRevisitTime = $("#startRevisitTime").val();  //开始回访时间
    var endRevisitTime = $("#endRevisitTime").val();  //结束回访时间
    var revisit = $("#revisit").val();  //随访人员
    var createName = $("#createName").val();  //创建人
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorRevisit/total",
        data: {
            reach: reach,
            revisitStatus: revisitStatus,
            startRevisitTime: startRevisitTime,
            endRevisitTime: endRevisitTime,
            revisit: revisit,
            createName: createName
        },
        dataType: "json",
        success: function (data) {
            $("#totalNum").html(data[0].countNum);//总人数
            $("#planned").html(data[1].countNum);//待计划
            $("#finished").html(data[2].countNum);//已完成
            $("#unfinished").html(data[3].countNum);//未完成
            $("#tocancel").html(data[4].countNum);//取消
        }
    });
};

DoctorRevisit.inquire = function () {//人数统计 + 表格刷新
    DoctorRevisit.search();
    DoctorRevisit.total();
};

DoctorRevisit.formParams = function () {// 查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['reach'] = $("#doctorTcInputPublic", parent.document).val().trim();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['outpatientName'] = $("#outpatientName", parent.document).val();
    queryData['revisitStatus'] = $("#revisitStatus").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['createName'] = $("#createName").val();
    return queryData;
};

DoctorRevisit.totalAllNumber = function (obj) {//根据点击上面状态刷新表格
    $("#revisitStatus").val(obj);
    DoctorRevisit.inquire();
};

DoctorRevisit.resetSearch = function () {//重置
    $("#doctorTcInputPublic", parent.document).val("");
    $("#startRevisitTime").val("");
    $("#endRevisitTime").val("");
    $("#revisit").val("");
    $("#createName").val("");
    DoctorRevisit.inquire();
};

DoctorRevisit.time = function () {//初始化赋值时间 + 请求数据
    $("#startRevisitTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    $("#revisitTimeTwo").val(setDateTime(new Date(), 0));
    var seleteDoctorValue = $('#loginName').val();
    $("#revisit option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
};

$("#doctorTcSearchAll", parent.document).click(function () {//主页搜索前台
    DoctorRevisit.inquire();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#DoctorRevisitTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}

$(function () {
    var defaultColunms = DoctorRevisit.initColumn();
    var table = new BSTable(DoctorRevisit.id, "/doctorRevisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DoctorRevisit.formParams());
    DoctorRevisit.table = table.init();
    checkDate = function () {
        DoctorRevisit.inquire();
    };
    DoctorRevisit.time();
    DoctorRevisit.totalAllNumber();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {
        DoctorRevisit.inquire();
    });
    phonePrivileges();
});